﻿<?php
/*
 * @Author Akos Szabo - Axi
 * @Created on Jul 5, 2011
 * @Project: ujej
 * @Description: This is the main file for the register function
 * 
 */
 
include_once 'mysql_dbase.php'; //DB handler include
include_once 'user_handler.php'; //User class
include_once 'tools.php'; //Collection of tools, like get list from DB
include 'captcha/captcha.php';//captcha rutinok
session_start();
 

 //check if CAPTCHA is OK
$resMsg = "" ;
$mailMsg = "";
$pwMsg = "";
$pw2Msg = "";
$sporMsg = "";
$sport2Msg = "";
$captchaMsg = "";
$debugMsg = "POST ertekek: </br>";

//User data
$umail = $_POST['umail'];
$selectedSport = $_POST['sportList'];
$masSport =  $_POST['masSport'];

//create db handler
//$dbh = new mysql_dbase("ujej", "localhost", "ujej", "Ujej123");
$dbh = new mysql_dbase();
$dbh->connect();
$sportslist = getSportList($selectedSport);

$regpage_mainmenu = getHomeMenu();


foreach( $_POST as $mEntry )
{
	$debugMsg = $debugMsg . $mEntry . "</br>";
}


$formOk = 1;

if($_POST['submit_reg'])
{
	//Captcha ellenörzés
	if($_POST['captcha_num']==$_SESSION['captcha_num'])
	{
		$resMsg ="RENDBEN: "."post:".$_POST['captcha_num']."==".$_SESSION['captcha_num']  ;
	}
	else
	{
		$formOk = 0;
		$resMsg ="Hibás Captcha: "."post:".$_POST['captcha_num']."<>".$_SESSION['captcha_num']   ;
		$captchaMsg = " << Hibás megadás!";
	}	
	
	if($umail=="")
	{
		$formOk = 0;
		$mailMsg = " << Üres az e-mail mező!";
	}
	else
	{
		//mail ellenörzés. 
		if(!filter_var($umail, FILTER_VALIDATE_EMAIL))
  		{
  			//echo "E-mail is not valid";
  			$mailMsg = " << Nem jó formátum!";
  			$formOk = 0;
  		}
		else
		{
			//echo "E-mail is valid";			
			$mailMsg = "OK mail >> " . $umail;
		}
		
	}
	
	//jelszo ellenorzes
	if( $_POST['upw']=="")
	{
		$formOk = 0;
		$pwMsg = " << Üres jelszó mező!";		
	}
	if( $_POST['upw2']=="")
	{
		$formOk = 0;
		$pw2Msg = " << Üres jelszó megerősítés!";		
	}

	if( $_POST['upw']<>$_POST['upw2'])
	{
		$formOk = 0;
		$pw2Msg = " << Nem azonos a jelszó!";		
	}

	
	if( $_POST['masSport']=="")
	{
		$formOk = 0;
		$sportMsg = " << Nincs Sport megadva!";		
	}
	
}//end of if POST
else
{
	$formOk = 0;
}


//check if mail is used
if( $formOk == 1)
{
	//check mail in DB and insert all the value if mail is valid
	if( $dbh->isMailUsed( $umail) )
	{
		$formOk = 0;
		$mailMsg = " << Ez már foglalt!";
	}
}

//insert data, register the user
if( $formOk == 1)
{

$rpw = $_POST['upw'];
$oUser = new user_handler($dbh);
$ujejId = $oUser->registerUser( $umail, $_POST['upw'], $_POST['masSport'] );
echo "reg id=".$ujejId."; ";
$_SESSION['ujej_id'] = $ujejId; 

}
 
 
//include the template page of the main page
if($formOk == 0)
{
	include "register_t.php"; 
}
else
{
	$_SESSION['ujej_timeout'] = time() + g_TIMEOUT;
	include "register_ok_t.php";
	//echo "!!!OK!!!";
}

flushLog();
?>
